template.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use client";
  2. import { usePathname } from "@/i18n/routing";
  3. import { AnimatePresence, motion } from "framer-motion";
  4. import { LayoutRouterContext } from "next/dist/shared/lib/app-router-context.shared-runtime";
  5. import { ReactNode, useContext, useRef } from "react";
  6. // const Template = ({ children }: { children: ReactNode }) => {
  7. // const key = usePathname();
  8. // return (
  9. // <AnimatePresence mode="popLayout">
  10. // <motion.div
  11. // layout
  12. // key={key}
  13. // initial={{ opacity: 0 }}
  14. // animate={{ opacity: 1 }}
  15. // exit={{ opacity: 0 }}
  16. // transition={{
  17. // duration: 1,
  18. // ease: "easeOut",
  19. // }}
  20. // className={"h-[100%]"}
  21. // >
  22. // {children}
  23. // </motion.div>
  24. // </AnimatePresence>
  25. // );
  26. // };
  27. function FrozenRouter(props: { children: ReactNode }) {
  28. const context = useContext(LayoutRouterContext ?? {});
  29. const frozen = useRef(context).current;
  30. return (
  31. <LayoutRouterContext.Provider value={frozen}>{props.children}</LayoutRouterContext.Provider>
  32. );
  33. }
  34. const Template = ({ children }: { children: ReactNode }) => {
  35. const key = usePathname();
  36. return (
  37. <AnimatePresence mode="wait">
  38. <motion.div
  39. key={key}
  40. initial={{ opacity: 0 }}
  41. animate={{ opacity: 1 }}
  42. transition={{
  43. duration: 0.3,
  44. ease: "easeInOut",
  45. }}
  46. layout="position"
  47. className={"h-[100%]"}
  48. >
  49. {children}
  50. </motion.div>
  51. </AnimatePresence>
  52. );
  53. };
  54. export default Template;